<?php

// Init owncloud

require_once(OC_App::getAppPath('resumes') . "/lib/helper.php");
require_once(OC_App::getAppPath('resumes') . "/lib/resume.php");
require_once(OC_App::getAppPath('resumes') . "/lib/searchresume.php");

OCP\JSON::callCheck();
$companyid = 0;
if(!OC_User::isLoggedIn()) {
   echo '您需要先登录';
   exit;
}

$user = PFactory::getMasterUser();
$companyid = $user->id;
if(!$companyid) {
   echo '您需要先登录';
   exit;
}

$source = JRequest::getString('source', '51job');
$resumecode = JRequest::getString('resumecode', '');

if(empty($resumecode)) {
   echo '简历未找到';
   exit;
}

$downloadedfile_dir = "/search/downloaded/". $source . "/" . $resumecode . "/resume.html";
$undownloadfile_dir = "/search/undownload/". $source . "/" . $resumecode . "/resume.html";

$model = new PResumesModelSearchResume();
$rtn = $model->getSearchResultByCode($companyid,$resumecode,$source);
$resumefile = $rtn['is_saved'] == 1 ? $downloadedfile_dir : $undownloadfile_dir;

if(!file_exists(OC::$SERVERROOT."/data/" . $resumefile)){

   $command = "/usr/bin/node " . realpath(JPATH_ROOT."/../cloud/daemon/thirdtools/synctool.js");

   try {
      $argv = " --uid " . $companyid . " --type " . $source . " --action viewresumes --ids " . $resumecode;
      // try to view resume now
      $output = execTimeout($command . $argv, 60);
   }
   catch(Exception $e) {
      echo ("系统出现错误，请报告客服！: " . $e);
      exit;
   }
   
   $rtn = $model->getSearchResultByCode($companyid,$resumecode,$source);
   $resumefile = $rtn['is_saved'] == 1 ? $downloadedfile_dir : $undownloadfile_dir;
   
   if(!file_exists(OC::$SERVERROOT."/data/" . $resumefile)){
      echo '简历未找到';
      exit;
   }
}

$content = file_get_contents(OC::$SERVERROOT."/data/" . $resumefile);
$content = preg_replace("/__ppp_display_none__/", "none", $content);
echo $content;
